﻿using Microsoft.Office.Interop.Excel;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;


namespace NovoSerDes.A2B.Func
{
    public class ActionData
    {
        public string Instr { get; set; }
        public string AddrWidth { get; set; }
        public string DataWidth { get; set; }
        public string Len { get; set; }
        public string Addr { get; set; }
        public string I2CAddr { get; set; }
        public string AddrIncr { get; set; }
        public string CellName { get; set; }
        public string ParamName { get; set; }
        public string[] Values { get; set; }
    }
    internal class XmlPars
    {
        System.Data.DataTable table = new System.Data.DataTable();
        public XmlPars() {
            table.Columns.Add("Mode", typeof(string));
            table.Columns.Add("AddrWidth", typeof(string));
            table.Columns.Add("DataWidth", typeof(string));
            table.Columns.Add("Len", typeof(string));
            table.Columns.Add("Address", typeof(string));
            table.Columns.Add("I2CAddr", typeof(string));
            table.Columns.Add("AddrIncr", typeof(string));
            table.Columns.Add("CellName", typeof(string));
            table.Columns.Add("ParamName", typeof(string));
            table.Columns.Add("Values", typeof(string)); // Store the values as a string
        }

        public System.Data.DataTable ParsXmlFile(string xmlPath) {
            XDocument xmlDoc = XDocument.Load(xmlPath);
            string IICAdress = xmlDoc.Root?.Attribute("IC_Address")?.Value;
            List<ActionData> actions = xmlDoc.Descendants("action").Select(action => new ActionData
            {
                Instr = action.Attribute("instr")?.Value,               
                Len = action.Attribute("len")?.Value,               
                Addr = action.Attribute("addr")?.Value,
                I2CAddr = action.Attribute("i2caddr") != null ? action.Attribute("i2caddr").Value : IICAdress,
                AddrIncr = action.Attribute("AddrIncr")?.Value,
                CellName = action.Attribute("CellName")?.Value,
                ParamName = action.Attribute("ParamName")?.Value,
                Values = action.Value.Trim().Split(' '),
                AddrWidth = action.Attribute("addr_width") != null ? action.Attribute("addr_width").Value : ((action.Attribute("addr")?.Value.ToInt().ToString("X").Length + 1) / 2 ).ToString(),
                DataWidth = action.Attribute("data_width") != null ? action.Attribute("data_width").Value : ((action.Value.Trim().Split(' ')[0].Length) / 2 ).ToString()
            }).ToList();
            // Populate the DataTable with the actions data
            foreach (var action in actions)
            {
                string valueWithPrefix = string.Join(",",action.Values.Select(value => "0x" + value));
                table.Rows.Add(action.Instr, action.AddrWidth, action.DataWidth, action.Len, action.Addr == null ? null : "0x" + action.Addr.ToInt().ToString("X"), action.I2CAddr, action.AddrIncr, action.CellName, action.ParamName, valueWithPrefix);
            }
            return table;

        }

    }
}
